<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\WebPage;
use StudyBuddy\Template\Urhere;

/**
 * Controller for rendering
 * the "Members" page
 *
 * If request is by Ajax, it returns only the content
 * of the main area, paginated, sorted and with pagination
 * links if necessary
 *
 */
class Viewresourcecats extends WebPage {

    protected $pagerPath = '/listrcats';
    protected $layoutID = 1;
    protected $qtab = 'resources';

    protected function main() {

        $this->setTitle()
                ->makeTopTabs()
                ->makeRCatsBody()
                ->makeRecentActivites()
                ->makeRecentTags()
                ->makeTutorListing();
    }

    protected function setTitle() {
        $title = 'Resource';
        $this->aPageVars['title'] = $title;
        $this->aPageVars['qheader'] = '<h1>' . $title . '</h1>';

        return $this;
    }

    protected function makeTopTabs() {
        $guest = null;
        if($this->oRegistry->Viewer->isGuest()) {
            $guest['link_ask'] = '#'; // 13
            $guest['link_ask_click'] = ' onclick="oSL.getQuickRegForm()"'; //14
        }
        d('cp');
        $tabs = Urhere::factory($this->oRegistry)->get('tplToptabs', $this->qtab, $guest);
        $this->aPageVars['topTabs'] = $tabs;

        return $this;
    }
    
    protected function makeRecentTags() {

        $aUserTags = $this->oRegistry->Viewer['a_f_t'];
        if (!empty($aUserTags)) {
            $s = $this->getSortedRecentTags($aUserTags);
        } else {
            $s = $this->oRegistry->Cache->get('qrecent');
        }

        $tags = \tplBoxrecent::parse(array('tags' => $s, 'title' => $this->_('Recent Tags')));
        d('cp');
        $this->aPageVars['tags'] = $tags;

        return $this;
    }

    //hoangto tutor listing
    protected function makeTutorListing() {

        $s = $this->getSortedTutors();
        $tutors = \tplTutorBox::parse(array('tutors' => $s, 'title' => $this->_('Tutor Listing')));
        d('cp');
        $this->aPageVars['tutors'] = $tutors;

        return $this;
    }

    //hoangto tutor listing
    protected function makeRecentActivites() {

        $s = $this->getSortedActivities();
        $tutors = \tplActivityBox::parse(array('ractivites' => $s, 'title' => $this->_('Recent activities')));
        d('cp');
        $this->aPageVars['activities'] = $tutors;

        return $this;
    }
    
    protected function getSortedRecentTags(array $aUserTags, $type = 'recent') {

        $limit = 9;
        if ('unanswered' === $type) {
            //$cur = $this->oRegistry->Mongo->UNANSWERED_TAGS->find(array(), array('tag', 'i_count'))->sort(array('i_ts' => -1))->limit($limit);
            $cur = $this->oRegistry->Mongo->UNANSWERED_TAGS->find(array(), array('tag', 'i_count'))->sort(array('i_count' => -1))->limit($limit);
        } else {
            //$cur = $this->oRegistry->Mongo->QUESTION_TAGS->find(array('i_count' => array('$gt' => 0)), array('tag', 'i_count'))->sort(array('i_ts' => -1))->limit($limit);
            $cur = $this->oRegistry->Mongo->QUESTION_TAGS->find(array('i_count' => array('$gt' => 0)), array('tag', 'i_count'))->sort(array('i_count' => -1))->limit($limit);
        }

        d('got ' . $cur->count(true) . ' tag results');
        $aTags = iterator_to_array($cur);

        d('aTags: ' . print_r($aTags, 1));

        if (!empty($aTags)) {
            usort($aTags, function($a, $b) use ($aUserTags) {
                        return (in_array($a['tag'], $aUserTags)) ? -1 : 1;
                    });
        };

        d('$aTags now: ' . print_r($aTags, 1));
        $html = ('unanswered' === $type) ? \tplUnanstags::loop($aTags) : \tplLinktag::loop($aTags);

        d('html recent tags: ' . $html);

        return '<div class="tag"><ul>' . $html . '</ul></div>';
    }

    //hoangto tutor listing loop
    protected function getSortedTutors() {

        $cur = $this->oRegistry->Mongo->USERS->find(array('tmp_role' => 'tutor'))->sort(array('experience' => -1))->limit(9);

        $canTeach = '';

        $func = function(&$a) use($canTeach) {
                    $a['canTeach'] = $canTeach;
                };

        $tutors = \tplTutorListing::loop($cur, true, $func);

        return $tutors;
    }

    //hoangto tutor listing loop
    protected function getSortedActivities() {

        $act_link = '';
        $badges = '';

        $func = function(&$a) use($act_link, $badges) {
                    $a['act_link'] = $act_link;
                    $a['badges'] = $badges;
                };

        $cur = $this->oRegistry->Mongo->ACTIVITIES->find()->sort(array('i_ts' => -1))->limit(5);

        $tutors = \tplActivityList::loop($cur, true, $func);

        return $tutors;
    }

    protected function makeRCatsBody() {
        $aCat = array(
                'Notes and Mindmaps',
                'Textbook	solutions',
                'Exam	papers Solutions',
                'Suggested Study schedule',
                'Study Checklist',
                'Coming Soon'
            );
        if($this->oRegistry->Viewer->isAdmin()) {
            $aCat[] = '<a href="/uploadresource" title="Upload Resource">Upload New Resource</a>';
        } else {
            $aCat[] = '';
        }
        
        $sRcats = \tplRcats::parse($aCat, false);
        $this->aPageVars['body'] = $sRcats;
        d('cp');
        /**
         * In case of Ajax can just send out sQlist as result
         */
        return $this;
    }

}
